<html><head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>CreateMutex</title></head>
<body bgcolor="#FFFFDF" link="#009999" vlink="#006666" alink="#006666">
<font face="Arial" size="2"><p align="center"><b><font size="4">CreateMutex()</font></b></p>
<p><b>语法</b></p><blockquote>

Mutex = <font color="#006666"><b>CreateMutex</b></font>()</blockquote>

</blockquote>
<b>概要</b><br><blockquote>



Creates a new mutex object. The mutex is initially unlocked. 
<br>
<br>
The main objective of the mutex functions is thread synchronisation. They do not create 
too much overhead, but they only work within one program, not system-wide. 
A mutex is an object that can only be "owned" or locked by one thread at a time, 
so it is used to protect shared resources. Only the thread that has the mutex 
locked may access a certain file, memory area, ... 
<br>
See <a href="lockmutex.html">LockMutex()</a> and <a href="unlockmutex.html">UnlockMutex()</a> for locking/unlocking a mutex. 

@noparameter



</blockquote><p><b>返回值</b></p><blockquote>



The new mutext object, or zero if the mutex creation has failed. 

 

</blockquote><p><b>示例</b></p><blockquote>

<pre><font face="Courier New, Courier, mono"size="2">  <font color="#006666">; Run this code once as it is. You will see that the lines printed are</font>
  <font color="#006666">; mixed from between the threads. Now uncomment the Mutex functions and</font>
  <font color="#006666">; the strings are printed in order, because only one thread at a time</font>
  <font color="#006666">; has the right to execute the printing functions.</font>
  <font color="#006666">;</font>
  <b><font color="#006666">Procedure</font></b> <font color="#006666">WithoutMutex</font>(*Number)     
    <b><font color="#006666">Shared</font></b> Mutex
    
    <b><font color="#006666">For</font></b> a = 1 <b><font color="#006666">To</font></b> 5      
      <font color="#006666">;LockMutex(Mutex)    ; uncomment this to see the difference</font>
    
<font color="#006666">      PrintN</font>("Thread "+<font color="#006666">Str</font>(*Number)+": Trying to print 5x in a row:")
      <b><font color="#006666">For</font></b> b = 1 <b><font color="#006666">To</font></b> 5
<font color="#006666">        Delay</font>(50)
<font color="#006666">        PrintN</font>("Thread "+<font color="#006666">Str</font>(*Number)+" Line "+<font color="#006666">Str</font>(b))
      <b><font color="#006666">Next</font></b> b          
      
      <font color="#006666">;UnlockMutex(Mutex) ; uncomment this to see the difference</font>
    <b><font color="#006666">Next</font></b> a    
  <b><font color="#006666">EndProcedure</font></b>
  
<font color="#006666">  OpenConsole</font>()
  Mutex =<font color="#006666"> CreateMutex</font>()
  
  thread1 =<font color="#006666"> CreateThread</font>(@WithoutMutex(), 1)
<font color="#006666">  Delay</font>(25)
  thread2 =<font color="#006666"> CreateThread</font>(@WithoutMutex(), 2)
<font color="#006666">  Delay</font>(25)
  thread3 =<font color="#006666"> CreateThread</font>(@WithoutMutex(), 3)
  
<font color="#006666">  WaitThread</font>(thread1)
<font color="#006666">  WaitThread</font>(thread2)
<font color="#006666">  WaitThread</font>(thread3)
  
<font color="#006666">  Input</font>()
</font></pre>


</Blockquote><p><b>已支持操作系统 </b><Blockquote>所有</Blockquote></p><center><a href=index.html>Thread Index</a> - <a href=createsemaphore.html>CreateSemaphore()</a> -&gt;



</body></html>